using System;
using System.Collections;
using System.Text;
using System.Xml;

namespace InfoJetSoft.Service.Util
{
	
	class StringUtil
	{
		
		public static string replace( string str , int begin , int length , string replaceStr )
		{
			string left = str.Substring( 0 , begin );
			string right = str.Substring( begin + length );
			return left + replaceStr + right;
		}

		public static bool isXmlNameChar( char c )
		{
			if( Char.IsLetterOrDigit( c ) || c == '_' || c == '-' || c == '.' )
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		public static string formatLocation( string location )
		{
			int index = location.LastIndexOf( "/" );
			if( index > 0 )
			{
				return location.Substring( index + 1 );
			}
			index = location.LastIndexOf( "\\" );
			if( index > 0 )
			{
				return location.Substring( index + 1 );
			}
			return location;
		}

		public static string formatXmlField( string fieldValue )
		{
			fieldValue = fieldValue.Replace( "&", "&amp;" );
			fieldValue = fieldValue.Replace( "<", "&lt;" );
			fieldValue = fieldValue.Replace( ">", "&gt;" );
			return fieldValue;
		}

		public static string formatUrl( string url )
		{ 
			System.Text.RegularExpressions.MatchEvaluator matchEvaluator = new System.Text.RegularExpressions.MatchEvaluator(formatUrlMatch);
			return System.Text.RegularExpressions.Regex.Replace(url, @"[ ,!&+]", matchEvaluator);
		}

		private static string formatUrlMatch(System.Text.RegularExpressions.Match match)   
		{   
			char c = match.Value[0];   
			return String.Format("%{0:X2}", (int)c);
		}

		public static bool EqualsNotNull( string value1, string value2 )
		{
			if( value1 != null && value2 != null && value1.Equals( value2 ) )
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		public static bool IsNullOrEmpty( string str )
		{
			if( str == null || str.Length <= 0 )
			{
				return true;
			}
			else
			{
				return false;
			}
		}

	}
}
